<?php
  /**
  * Google reader to delicious
  *
  * To store bookmarks at one place. Moving all shared items of Google
  * Reader to delicious.
  * @license http://creativecommons.org/licenses/by-nc-sa/3.0/
  * @author Rick Mans <rickmans@gmail.com>
  * @version 1.0
  * @filesource
  */
  
  error_reporting(E_ALL | E_STRICT);
  
  /**
  * Settings for delicious
  */
  $deliciousApiURI                                =   "https://api.del.icio.us/v1/";
  $addPostURIPart                                 =   "posts/add?";
  $suggestTagsURIPart                             =   "posts/suggest?url=";
  
  /**
  * Enter your delicious username and password
  */
  $deliciousUser                                  =   "";
  $deliciousPassword                              =   "";
  
  /**
  * LongUrl settings
  */
  $longUrlURI                                     =   "http://api.longurl.org/v1/expand?url=";
  $longUrlFormat                                  =   "&format=php";
  
  /**
  * User Agent for curl, so services can identify
  */
  $curlUserAgent                                  =   "burningCat";
  
  /**
  * Settings for Google Reader
  */
  $googleReaderURI                                =   "http://www.google.com/reader/public/atom/user%2F02848039997193907980%2Fstate%2Fcom.google%2Fbroadcast";
  
  /**
  * Get the contents of the rssfeed of shared items in Google Reader
  */
  $curlGr                                         =   curl_init();

  /**
  * Settings for the curl session
  */
  curl_setopt($curlGr,                                CURLOPT_URL,              $googleReaderURI);
  curl_setopt($curlGr,                                CURLOPT_RETURNTRANSFER,   1);
  curl_setopt($curlGr,                                CURLOPT_CONNECTTIMEOUT,   0);
  curl_setopt($curlGr,                                CURLOPT_USERAGENT,        $curlUserAgent);

  $xmlGr                                          =   curl_exec($curlGr);

  /**
  * Close the session
  */
  curl_close($curlGr);

  /**
  * Load the xml data from Google Reader
  */
  $xmlObjGr = simplexml_load_string($xmlGr);

  /**
  * Loop through the entries and add each entry to delicious
  */
  foreach($xmlObjGr->entry as $futureBookmark){
    $urlTitle                                     =   urlencode($futureBookmark->title);
    
    foreach($futureBookmark->link->attributes() as $key=> $value){
      /**
      * Only links should be lookup
      */
      if($key == "href"){
        /**
        * Get the real url of the page (converts feedburners and url shorteners to full url
        */
        $curlLu                                   =   curl_init();

        /**
        * Set url for LongUrl request
        */
        $luUrl                                    =   $longUrlURI . urlencode($value) . $longUrlFormat;
     
        /**
        * Settings for the curl session
        */
        curl_setopt($curlLu,                          CURLOPT_URL,              $luUrl);
        curl_setopt($curlLu,                          CURLOPT_RETURNTRANSFER,   1);
        curl_setopt($curlLu,                          CURLOPT_CONNECTTIMEOUT,   1);
        curl_setopt($curlLu,                          CURLOPT_USERAGENT,        $curlUserAgent);
        
        $LuArray                                  =   unserialize(curl_exec($curlLu));

        /**
        * Close the session
        */
        curl_close($curlLu);

        $curlDelTags = curl_init();

        /**
        * Set url for Delicious recommended tags
        */
        $url                                      =   $deliciousApiURI . $suggestTagsURIPart . $LuArray["long_url"];
    
        /**
        * Settings for the curl session
        */
        curl_setopt($curlDelTags,                     CURLOPT_URL,              $url);
        curl_setopt($curlDelTags,                     CURLOPT_CONNECTTIMEOUT,   2);
        curl_setopt($curlDelTags,                     CURLOPT_RETURNTRANSFER,   1);
        curl_setopt($curlDelTags,                     CURLOPT_USERPWD,          $deliciousUser .":". $deliciousPassword);
        curl_setopt($curlDelTags,                     CURLOPT_USERAGENT,        $curlUserAgent);
        
        $delTags                                  =   curl_exec($curlDelTags);
        
        curl_close($curlDelTags);
        
        /**
        * Load the xml data from Google Reader
        */
        $xmlObjDelTags                            =   simplexml_load_string($delTags);
        
        $tags                                     =   array();
        
        foreach($xmlObjDelTags->recommended as $tag){
          $tags[]                                 =   $tag;
        }
        
        /**
        * Close the session
        */
        
      }
    }

    /**
    * Setup the curl session for delicious
    */
    $curlDel                                      =   curl_init();

    /**
    * Set url for adding bookmark to Delicious
    */
    $url                                          =   $deliciousApiURI . $addPostURIPart ."&url=". $LuArray["long_url"] ."&description=". $urlTitle ."&tags=". urlencode(implode(" ", $tags));

    /**
    * Settings for the curl session
    */
    curl_setopt($curlDel,                             CURLOPT_URL,              $url);
    curl_setopt($curlDel,                             CURLOPT_CONNECTTIMEOUT,   5);
    curl_setopt($curlDel,                             CURLOPT_RETURNTRANSFER,   1);
    curl_setopt($curlDel,                             CURLOPT_USERPWD,          $deliciousUser .":". $deliciousPassword);
    curl_setopt($curlDel,                             CURLOPT_USERAGENT,        $curlUserAgent);
    
    curl_exec($curlDel);
    

    /**
    * Close the session
    */
    curl_close($curlDel);
  }
?>
